<template>
    <el-button
               type="primary"
               class="btn commonButton"
               @click="handleClick"
               size="small"
               :autofocus="autofocus"
               :loading="loading"
               :icon="icon"
               :round="round"
               :circle="circle"
               :nativeType="nativeType"
               :disabled="disabled">
            <span v-if="text && text!==''">{{text}}</span>
        </el-button>
</template>
<script>
    import Service from '../../../services/common.service'
    export default {
        lock: Boolean,
        _params: Object,
        _url: String,
        clickTime: Number,
        data() {
            return {
                icon:''
            }
        },
        created: function () {
            this.icon = this.hasIcon ? 'el-icon-search' : '';
        },
        methods: {
            handleClick(evt) {
                const me = this;
                if (me.lock) {
                    return;
                }
                if (me.limitTime && (new Date().getTime() - me.clickTime < me.limitTime)) {
                    return;
                }
                me.clickTime = new Date().getTime();
                me.$emit('click', evt, me);
                if (me.autopost) {
                    me.post();
                }
            },
            post(url, params) {
                const me = this;
                if (me.lock) {
                    return;
                }
                me._params = params ? params : me.params;
                me._url = url ? url : me.url;

                if (me._url && me._params) {
                    me.lock = true;
                    Service.post(me._url, me._params).then(res => {
                        if (res.data.succeeded == true) {
                            me.$emit('callback', res);
                        }
                        me.lock = false;
                    })
                }

            }
        },
        props: {
            size: String,
            nativeType: {
                type: String,
                default: 'button'
            },
            loading: Boolean,
            disabled: Boolean,
            plain: Boolean,
            autofocus: Boolean,
            round: Boolean,
            circle: Boolean,
            text: String,
            hasIcon: Boolean,
            url: String,
            params: Object,
            autopost: {
                type: Boolean,
                default: false
            },
            limitTime: {
                type: [Number, Function],
                default: 0
            }
        },
    }
</script>
